// 页面标题和 头部固定和 侧边栏logo的控制的模块
import defaultSettings from '@/settings'
import { getThemeInfo, setThemeInfo } from '@/utils/auth'
const { showSettings, fixedHeader, sidebarLogo, theme } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  // 优先读取本地的颜色
  theme: getThemeInfo() || theme
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // console.log(key, value)
    // hasOwnProperty 是检测对象中是否有某个属性
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
    // 如果更新的是theme属性, 将theme持久化存储
    if (key === 'theme') {
      setThemeInfo(value)
    }
  },
  // 修改主题色的mutation
  // changeTheme(state, val) {
  //   state.theme = val
  // }
  // 还原主题色的mutation
  resetTheme(state) {
    state.theme = theme
  }
}

const actions = {
  changeSetting({ commit }, data) {
    console.log(data)
    commit('CHANGE_SETTING', data)
    // {key: , value:}
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
